/******************* (C) COPYRIGHT 2022 CHU WEI TENG ***********************************************
**
**------------------ File Info ---------------------------------------------------------------------
** File name:      dwin_edit_gbk.c
** modified Date:  2022-05-10
** Last Version:   V1.0
** Descriptions: 
** 
**------------------ Modfication  log --------------------------------------------------------------
** Date       Who            Rev        Comments
----------------------------------------------------------------------------------------------------
   22/05/10  chuwt@qq.com    V1.00      created

****************************************************************************************************/
#include <string.h>
#include "dwin_edit_gbk.h"
#include "dwin_trans.h"
#include "dwin_adapt.h"

/* Private define----------------------------------------------------------------------------------*/

/* Private typedef---------------------------------------------------------------------------------*/
static ret_t edit_gbk_event_dispatch(dwin_widget_t* edit, void* ctx);
dwin_widget_vtable_t edit_gbk_vt = 
{
    .obj_size =  sizeof(dwin_edit_gbk_t),
    .dispatch = edit_gbk_event_dispatch,
};
/* Private variables-------------------------------------------------------------------------------*/

/* public function---------------------------------------------------------------------------------*/
dwin_widget_t* dwin_edit_gbk_create(dwin_widget_t* parent, uint16_t addr, uint16_t attr, uint16_t size)
{
    dwin_widget_t* edit= NULL;

    if (size > GBK_MAX_CNT)
    {
        size = GBK_MAX_CNT;
    }
    edit = dwin_widget_create(parent, &edit_gbk_vt, size);

    if (edit != NULL)
    {
        edit->active = 1;
        edit->addr = addr;
        edit->attr = attr;  
        edit->size = size;          
        edit->widget_type = DWIN_EDIT_GBK;
    }
    return edit;
}

ret_t dwin_edit_gbk_write(dwin_widget_t* edit)
{
    return dwin_widget_write(edit);
}

ret_t dwin_multi_edit_gbk_write(dwin_widget_t** edit_arry, uint8_t count)
{
    return dwin_multi_widget_write(edit_arry, count);
}

/* Private function-------------------------------------------------------------------------------*/
static ret_t edit_gbk_event_dispatch(dwin_widget_t* edit, void* ctx)
{
    dwin_edit_gbk_t* obj = (dwin_edit_gbk_t *)edit;
    data_pack_t*     pack = (data_pack_t *)ctx;
    uint8_t          min = DWIN_MIN(pack->data[0], edit->size);

    memcpy(obj->gbk, &pack->data[1], min);
    obj->gbk[min] = 0;
    obj->gbk_len = strnlen(obj->gbk, sizeof(obj->gbk));

   if (edit->func != NULL)
   {
       edit->func(edit);
   }
    return RET_OK;
}
